package cn.piggy.gateway.core.common;

import cn.piggy.gateway.core.auth.Auth;
import cn.piggy.gateway.core.auth.AuthService;
import cn.piggy.gateway.core.client.Request;
import cn.piggy.gateway.core.http.HttpStatement;
import cn.piggy.gateway.core.proxy.MyGenericReference;
import cn.piggy.gateway.core.proxy.jdk.ObjectProxyFactoryManager;
import cn.piggy.gateway.core.session.GatewaySession;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class CommonUtil {
    private CommonUtil(){}
    public static Map<Long, Request> futures = new ConcurrentHashMap<>();
    public static AtomicLong aid = new AtomicLong(0);
    public static String strategy = "roundrobin";
    public static int retries = 0;
    private static final Random random = new Random();
    public static String getAddress(LinkedList<String> list){
        int size = list.size();
        int i = random.nextInt(size);
        return list.get(i);
    }

    public static Object[] getParams(String parameterNames, Map<String, Object> params, String parameterTypes) throws Exception {
        String[] parameterTypeArray = parameterTypes.split(",");
        if(parameterTypeArray.length==1){
            if(SimpleTypeUtil.isSimpleType(parameterTypeArray[0])){
                String param = params.get(parameterNames).toString();
                String type = parameterTypeArray[0];
                if(!"java.lang.String".equals(type)){
                    Class<?> clazz = Class.forName(type);
                    Method methodObj = clazz.getMethod("valueOf", String.class);
                    Object arg = methodObj.invoke(null, param);
                    return new Object[]{arg};
                }else{
                    return new Object[]{param};
                }
            }else{
                return new Object[]{params};
            }
        }
        throw new Exception("???");
    }


    private static final ObjectProxyFactoryManager objectManager = new ObjectProxyFactoryManager();
    private static final Map<String, HttpStatement> httpStatementMap = new HashMap<>();

    public static MyGenericReference getGenericReference(String uri, GatewaySession gatewaySession) {
        return objectManager.getProxyObject(uri, gatewaySession);
    }

    public static void addHttpMapper(HttpStatement httpStatement) {
        String uri = httpStatement.getUri();
        if (objectManager.isExist(uri)) {
            return;
        }
        objectManager.produceFactory(uri);
        httpStatementMap.put(uri, httpStatement);

    }

    public static HttpStatement getHttpStatement(String uri) {
        return httpStatementMap.get(uri);
    }


    private final static Auth auth = new AuthService();

    public static boolean authValidate(String token,String role) {
        return auth.validate(token, role);
    }
}
